package com.sansecy.distanceeditor;

import android.content.Context;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;

public class DistanceEditor extends LinearLayout implements View.OnClickListener{
    private TextRelativeLayout mText1;
    private TextRelativeLayout mText2;
    private TextRelativeLayout mText3;
    private TextRelativeLayout mText4;

    private int mCursorPosition;
    private Handler mHandler;
    private boolean isShow = false;
    private Context mContext;

    public DistanceEditor(Context context) {
        super(context);
    }

    public DistanceEditor(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init(mContext);
        mHandler = new Handler();
    }

    private void init(Context context) {
        View rootView = View.inflate(context, R.layout.distance_editor_layout, this);
        mText1 = rootView.findViewById(R.id.text1);
        mText2 = rootView.findViewById(R.id.text2);
        mText3 = rootView.findViewById(R.id.text3);
        mText4 = rootView.findViewById(R.id.text4);
        mText1.setOnClickListener(this);
        mText2.setOnClickListener(this);
        mText3.setOnClickListener(this);
        mText4.setOnClickListener(this);
        mText1.getTextView().setText("1");
        mText2.getTextView().setText("2");
        mText3.getTextView().setText("3");
        mText4.getTextView().setText("4");
    }

    @Override
    public void onClick(View v) {
        if (v == mText1) {
            mCursorPosition = 0;
        }
        if (v == mText2) {
            mCursorPosition = 1;
        }
        if (v == mText3) {
            mCursorPosition = 2;
        }
        if (v == mText4) {
            mCursorPosition = 3;
        }
        setUnderlineDefault();
        hideAllCenterCursor();
        showCursorFirst();

        startCursor();
    }

    private void setUnderlineDefault() {
        mText1.getBottomCursor().setBackgroundColor(ContextCompat.getColor(mContext, R.color.black));
        mText2.getBottomCursor().setBackgroundColor(ContextCompat.getColor(mContext, R.color.black));
        mText3.getBottomCursor().setBackgroundColor(ContextCompat.getColor(mContext, R.color.black));
        mText4.getBottomCursor().setBackgroundColor(ContextCompat.getColor(mContext, R.color.black));
    }

    /**
     * 点击后提前显示光标位置
     */
    private void showCursorFirst() {
        switch (mCursorPosition) {
            case 0:
                mText1.getCenterCursor().setVisibility(VISIBLE);
                mText1.getBottomCursor().setBackgroundColor(ContextCompat.getColor(mContext, R.color.cursor));
                break;
            case 1:
                mText2.getCenterCursor().setVisibility(VISIBLE);
                mText2.getBottomCursor().setBackgroundColor(ContextCompat.getColor(mContext, R.color.cursor));
                break;
            case 2:
                mText3.getCenterCursor().setVisibility(VISIBLE);
                mText3.getBottomCursor().setBackgroundColor(ContextCompat.getColor(mContext, R.color.cursor));
                break;
            case 3:
                mText4.getCenterCursor().setVisibility(VISIBLE);
                mText4.getBottomCursor().setBackgroundColor(ContextCompat.getColor(mContext, R.color.cursor));
                break;
        }
        requestFocus();
        showSoftKeyboard(this);
        isShow = true;
    }

    private void startCursor() {
        mHandler.removeCallbacks(my);
        mHandler.postDelayed(my, 500);
    }

    private void hideAllCenterCursor() {
        mText1.getCenterCursor().setVisibility(INVISIBLE);
        mText2.getCenterCursor().setVisibility(INVISIBLE);
        mText3.getCenterCursor().setVisibility(INVISIBLE);
        mText4.getCenterCursor().setVisibility(INVISIBLE);
    }

    private Runnable my = new Runnable() {
        @Override
        public void run() {
            showOrHideCursor();
            mHandler.postDelayed(my, 500);
        }
    };

    public void cancel() {
        mHandler.removeCallbacks(my);
        mHandler = null;
    }

    private void showOrHideCursor() {
        switch (mCursorPosition) {
            case 0:
                if (isShow) {
                    mText1.getCenterCursor().setVisibility(INVISIBLE);
                    isShow = false;
                } else {
                    mText1.getCenterCursor().setVisibility(VISIBLE);
                    isShow = true;
                }
                break;
            case 1:
                if (isShow) {
                    mText2.getCenterCursor().setVisibility(INVISIBLE);
                    isShow = false;
                } else {
                    mText2.getCenterCursor().setVisibility(VISIBLE);
                    isShow = true;
                }
                break;
            case 2:
                if (isShow) {
                    mText3.getCenterCursor().setVisibility(INVISIBLE);
                    isShow = false;
                } else {
                    mText3.getCenterCursor().setVisibility(VISIBLE);
                    isShow = true;
                }
                break;
            case 3:
                if (isShow) {
                    mText4.getCenterCursor().setVisibility(INVISIBLE);
                    isShow = false;
                } else {
                    mText4.getCenterCursor().setVisibility(VISIBLE);
                    isShow = true;
                }
                break;
        }
    }

    public interface OnItemSelectedListener {
        void onItemSelected(TextRelativeLayout layout);
    }

    private OnItemSelectedListener mItemSelectedListener;

    public void setItemSelectedListener(OnItemSelectedListener itemSelectedListener) {
        mItemSelectedListener = itemSelectedListener;
    }

        public void showSoftKeyboard(View view) {
            if (view.requestFocus()) {
            }
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}
